<?php
/**
 * @category 	MenuController.php
 * @author 		Sung Hoon, Cho (akdddnet@gmail.com)
 * @since		2011. 10. 20.
 * @license		BSD License
 * @version		0.1
 */

global $CFG;
require_once $CFG->FW_DIR."/mvc/Controller.php";
require_once $CFG->CLASSES_DIR."/".$CFG->PATH->ADMIN."/menu/MenuEB.php";

class MenuController extends Controller {
	
	function __construct() {
		parent::__construct(get_class());	// 꼭 부모의 construct를 실행 해줘야됩니다.
	}
	
	function start() {
		global $CFG;
	
		$this->action = $CFG->PATH->WWW."/".$CFG->PATH->ADMIN."/".$this->module;
		
		$this->tpl->assign(array(
				 "action"=>$this->action
				,"module"=>$this->module
		));
	}
	
	
	private function getMenuIconSet() {
		global $CFG;
		require_once $CFG->FW_DIR."/utility/Dir.php";
	
		// 메뉴에 표시할 아이콘 목록
		$dir = new Dir();
		$iconset = $dir->getFileList($CFG->WEB_DIR."/images/iconset");
	
		return $iconset;
	}
	
	
	function index() {
		$this->page->class = 'menu';
		$this->page->title = '메뉴 관리';
		
		$dto = new DTO();
		$dto = $this->dao->index($dto);
		
		$list = $dto->list;
		
		if($list) {
			foreach($list as $board) {
				$board->date = date("Y-m-d", strtotime($board->ba_cdate));
			}
		}
		
		
		$this->tpl->assign(array(
				"list"=>$list
		));
	}
	
	
	
	function add() {
		$this->page->class = 'menu_add';
		$this->page->title = '메뉴 추가';
		
		$iconset = $this->getMenuIconSet();
		
		$this->tpl->assign(array(
					"list"=>$list
					,"iconset"=>$iconset
		));
	}
	
	
	function doAdd() {
		$param = new Parameter(false);
		
		
		$validate = new Validate();
		$validate->add("required", $param->getParameter($this->module."_name"), "메뉴 명");
		$validate->add("required", $param->getParameter($this->module."_url"), "URL");
		$validate->add(array("required", "number"), $param->getParameter($this->module."_external"), "새창");
		
		
		if($validate->validate()) {
			$dto = new DTO();
			
			$mn_icon = $param->getParameter($this->module."_icon");
			if(Common::isNullOrEmpty($mn_icon)) {
				$mn_icon = "default.png";
			}
				
			$eb = new MenuEB();
			$eb->mn_name = $param->getParameter($this->module."_name");
			$eb->mn_icon = $mn_icon;
			$eb->mn_url = $param->getParameter($this->module."_url");
			$eb->mn_external = $param->getParameter($this->module."_external");
							
			$dto->eb = $eb;
				
				
			$result = $this->dao->doAdd($dto)->rc;
			if($result > 0) {
				global $CFG;
				
				$message = "정상적으로 추가 되었습니다.";
				$return_url = $CFG->PATH->WWWADMIN."/".$this->module;
				
				$menu_refresh_url = $CFG->WWW."/admin/menu/externalMenuList";
		
				if(Common::isAjax()) {
					Javascript::printJSON(array('code'=>Javascript::getSuccessCode(), 'msg'=>$message, 'return_url'=>$return_url, 'menu_refresh_url'=>$menu_refresh_url));
				} else {
					Javascript::getAlertMove($message, $return_url);
				}
		
			} else {
				Javascript::getAlertWithAjax("추가시 문제가 발생하였습니다.");
			}
		} else {
			Javascript::getAlertWithAjax($validate->getErrorMessage());
		}
	}
	
	
	function view($mseq) {
		if(!Common::isNullOrEmpty($mseq)) {
			$dto = new DTO();
			$eb = new MenuEB();
			$eb->mn_seq = $mseq;
			$dto->eb = $eb;
				
			$result = $this->dao->view($dto)->obj;
				
			if(!Common::isNullOrEmpty($result)) {
				global $CFG;
				
				// 링크
				if($result->mn_external) {
					$result->url = $result->mn_url;
				} else {
					$result->url = $CFG->PATH->WWW."/".$result->mn_url;
				}
				
				
		
				$this->tpl->assign(array(
						"result"=>$result	
				));
			} else {
				Javascript::getAlertWithAjax("존재하지 않는 메뉴 입니다.");
					
			}
		} else {
			Javascript::getAlertWithAjax("메뉴 고유번호가 전달되지 않았습니다.");
		}
	}
	
	
	
	function doDelete($mseq) {
	
		if(!Common::isNullOrEmpty($mseq)) {
			$dto = new DTO();
			$eb = new MenuEB();
			$eb->mn_seq = $mseq;
				
			$dto->eb = $eb;
			$result = $this->dao->doDelete($dto)->rc;
			if($result > 0) {
				global $CFG;
	
				$message = "삭제 되었습니다.";
				$return_url = $CFG->PATH->WWWADMIN."/".$this->module;
				$menu_refresh_url = $CFG->WWW."/admin/menu/externalMenuList";
				
				if(Common::isAjax()) {
					Javascript::printJSON(array('code'=>Javascript::getSuccessCode(), 'msg'=>$message, 'return_url'=>$return_url, 'menu_refresh_url'=>$menu_refresh_url));
				} else {
					Javascript::getAlertMove($message, $return_url);
				}
				
			} else {
				Javascript::getAlertWithAjax("삭제시 문제가 발생하였습니다.");
			}
		} else {
			Javascript::getAlertWithAjax("메뉴 고유번호가 전달되지 않았습니다.");
		}
	
	}
	
	
	
	function modify($mseq) {
	
		if(!Common::isNullOrEmpty($mseq)) {
			$dto = new DTO();
			$eb = new MenuEB();
			$eb->mn_seq = $mseq;
			$dto->eb = $eb;
				
			$result = $this->dao->view($dto)->obj;
			
			$this->page->class = 'menu_add';
			$this->page->title = $result->mn_menu.' 메뉴 수정';
				
			if(!Common::isNullOrEmpty($result)) {
				// 아이콘set가져오기
				$iconset = $this->getMenuIconSet();
	
				$this->tpl->assign(array(
						"result"=>$result
						,"iconset"=>$iconset
				));
			} else {
				Javascript::getAlertWithAjax("존재하지 않는 메뉴 입니다.");
			}
		} else {
			Javascript::getAlertWithAjax("메뉴 고유번호가 전달되지 않았습니다.");
		}
	
	}
	
	
	
	function doModify() {
		
		$param = new Parameter(false);
		
		$validate = new Validate();
		$validate->add("required", $param->getParameter($this->module."_mseq"), "메뉴 고유번호");
		$validate->add("required", $param->getParameter($this->module."_name"), "메뉴 명");
		$validate->add("required", $param->getParameter($this->module."_url"), "URL");
		$validate->add(array("required", "number"), $param->getParameter($this->module."_external"), "새창");
		$validate->add(array("required", "number"), $param->getParameter($this->module."_order"), "정렬");
		
		
		if($validate->validate()) {
			$dto = new DTO();
			
			$mn_icon = $param->getParameter($this->module."_icon");
			if(Common::isNullOrEmpty($mn_icon)) {
				$mn_icon = "default.png";
			}
				
			$eb = new MenuEB();
			$eb->mn_seq = $param->getParameter($this->module."_mseq");
			$eb->mn_name = $param->getParameter($this->module."_name");
			$eb->mn_icon = $mn_icon;
			$eb->mn_url = $param->getParameter($this->module."_url");
			$eb->mn_external = $param->getParameter($this->module."_external");
			$eb->mn_order = $param->getParameter($this->module."_order");
							
			$dto->eb = $eb;
				
				
			$result = $this->dao->doModify($dto)->rc;
			//if($result > 0) {
				global $CFG;
				
				$message = "정상적으로 수정 되었습니다.";
				$return_url = $CFG->PATH->WWWADMIN."/".$this->module;
				
				$menu_refresh_url = $CFG->WWW."/admin/menu/externalMenuList";
		
				if(Common::isAjax()) {
					Javascript::printJSON(array('code'=>Javascript::getSuccessCode(), 'msg'=>$message, 'return_url'=>$return_url, 'menu_refresh_url'=>$menu_refresh_url));
				} else {
					Javascript::getAlertMove($message, $return_url);
				}
		
			//} else {
			//	Javascript::getAlertWithAjax("수정시 문제가 발생하였습니다.");
			//}
		} else {
			Javascript::getAlertWithAjax($validate->getErrorMessage());
		}
	}
	
	
	
	function getMenu($type, $seq) {
		if(!Common::isNullOrEmpty($type) && !Common::isNullOrEmpty($seq)) {
			$dto = new DTO();
			$eb = new MenuEB();
			
			$eb->mn_type = $type;
			$eb->mn_num = $seq;
			
			$dto->eb = $eb;
			$menu = $this->dao->getMenu($dto)->obj;
			
			// menu 정보가 있을 경우. 
			if(!Common::isNullOrEmpty($menu)) {
				return $menu;
			} else {
				return false;
			}
		} else {
			return null;
		}
	}
	
	
	function doexternalModify(DTO $dto) {
		
		// 이름도 필수값 검사를 해봐야될꺼 같은데.. 음;; 일단은 넘어가자
		if(Common::isNullOrEmpty($dto->eb->mn_icon)) {
			$dto->eb->mn_icon = "default.png";
		}
		
		return $this->dao->doExternalModify($dto);
	}
	
	function doExternalAdd(DTO $dto) {
		
		// 이름도 필수값 검사를 해봐야될꺼 같은데.. 음;; 일단은 넘어가자
		if(Common::isNullOrEmpty($dto->eb->mn_icon)) {
			$dto->eb->mn_icon = "default.png";
		}
		
		return $this->dao->doExternalAdd($dto);
	}
	
	
	function doExternalDelete(DTO $dto) {
		return $this->dao->doExternalDelete($dto);
	}
	
	
	function externalMenuList($isObjectReturn = false) {
		global $CFG;
		
		// 메뉴 정보 가져오기
		$menu = $this->dao->externalMenuList(new DTO())->list;
		
		// current 설정
		$module = $CFG->CALL_INFO->module;
		
		// 관리자 페이지 접근 했을 경우.
		if($CFG->CALL_INFO->admin) {
			$callinfo = "/".$CFG->PATH->ADMIN."/".$module."/";
		} else if(!strcmp($module, strtolower(DEFAULT_MODULE_NAME))){
			$callinfo = "/";
		} else {
			$callinfo = "/".$callinfo."/";
		}
		
		
		foreach($menu as $m) {
			$m->current = false;
		
			if(!strcmp($m->mn_url, $callinfo)) {
				$m->current = true;
			}
		}
		
		if($isObjectReturn) { 
			return $menu;
		} else {
			$this->tpl->assign(array("menuinfo"=>$menu));
			$this->tpl->define(array("main"=>"../_menu.html"));
		}
	}
}